package org.springframework.prospring.ticket.web.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.prospring.ticket.service.RegionManager;
import org.springframework.prospring.ticket.service.ClubManager;
import org.springframework.prospring.ticket.service.SurfaceTypeManager;
import org.springframework.prospring.ticket.service.FieldManager;
import org.springframework.prospring.ticket.web.views.JsonView;
import org.springframework.prospring.ticket.domain.location.Region;
import org.springframework.prospring.ticket.domain.ui.Response;
import org.springframework.prospring.ticket.domain.ui.ResponseMessage;
import org.springframework.prospring.ticket.domain.Club;
import org.springframework.prospring.ticket.domain.Field;
import org.springframework.prospring.ticket.domain.SurfaceType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Date;

@Controller
public class ClubAdminController {
    RegionManager regionManager;
    ClubManager clubManager;
    FieldManager fieldManager;
    SurfaceTypeManager surfaceTypeManager;

    public RegionManager getRegionManager() {
        System.out.println("===================== ClubAdminController.getRegionManager");
        return regionManager;
    }

    public void setRegionManager(RegionManager regionManager) {
        System.out.println("===================== ClubAdminController.setRegionManager");
        this.regionManager = regionManager;
    }


    public ClubManager getClubManager() {
        System.out.println("===================== ClubAdminController.getClubManager");
        return clubManager;
    }

    public void setClubManager(ClubManager clubManager) {
        System.out.println("===================== ClubAdminController.setClubManager");
        this.clubManager = clubManager;
    }


    public FieldManager getFieldManager() {
        return fieldManager;
    }

    public void setFieldManager(FieldManager fieldManager) {
        this.fieldManager = fieldManager;
    }

    public SurfaceTypeManager getSurfaceTypeManager() {
        return surfaceTypeManager;
    }

    public void setSurfaceTypeManager(SurfaceTypeManager surfaceTypeManager) {
        this.surfaceTypeManager = surfaceTypeManager;
    }

    @RequestMapping("/saveClub.dahtml")
    public ModelAndView saveClub(HttpServletRequest req, HttpServletResponse res, HttpSession session, Long provinceIdSelect, Long cityIdSelec, Long regionIdSelect, Long clubId, String razonSocial, String mail) {
        System.out.println("=========== org.springframework.prospring.ticket.web.controllers.ClubAdminController.saveClub  req = " + req+", provinceIdSelect="+provinceIdSelect+", cityIdSelec="+cityIdSelec+",regionIdSelect="+regionIdSelect+", razonSocial="+razonSocial+", mail="+mail);
        ModelAndView mav = new ModelAndView(JsonView.instance);

        try {

            String sitioweb = ServletRequestUtils.getStringParameter(req, "sitioweb");
            System.out.println("========== ClubAdminController.saveClub, sitioweb="+sitioweb);
        } catch(Exception ex) {
            ex.printStackTrace();
        }

        //String regionIdString = "CEN-001";
        //Region region = regionManager.findByBusinessPK(regionIdString);
        Region region = regionManager.findById(regionIdSelect);
        
        System.out.println("region = " + region);

        Club newClub = new Club();
        newClub.setName(razonSocial);
        newClub.setMail(mail);
        newClub.setRegion(region);
        try {
            clubManager.saveClub(newClub);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Response response = new Response(new ResponseMessage(ResponseMessage.SUCCESS_MESSAGE, "home.htm"));
        response.addData(new Date(System.currentTimeMillis()));
        mav.addObject(JsonView.JSON_ROOT, response);
        System.out.println("=========== org.springframework.prospring.ticket.web.controllers.ClubAdminController.saveClub  ModelAndView = " + mav);
        return mav;
    }

    //public ModelAndView updateClub(HttpServletRequest req, HttpServletResponse res, HttpSession session, Long id, Long cityIdSelec, Long regionIdSelect, Long clubId, String razonSocial, String mail) {
    @RequestMapping("/updateClub.dahtml")
    public ModelAndView updateClub(HttpServletRequest req, HttpServletResponse res, HttpSession session) {
        //System.out.println("=========== org.springframework.prospring.ticket.web.controllers.ClubAdminController.updateClub  req = " + req+", id="+id+", clubId="+clubId+",regionIdSelect="+regionIdSelect+", razonSocial="+razonSocial+", mail="+mail);
        System.out.println("=========== org.springframework.prospring.ticket.web.controllers.ClubAdminController.updateClub  req = " + req);

        String key = "";
        String value = "";
        try {
            Long fieldId = ServletRequestUtils.getLongParameter(req, "fieldId");
            System.out.println("========== ClubAdminController.saveClub, fieldId = " + fieldId);
            if (fieldId != null) {
                Field field = fieldManager.findField(fieldId);

                Long surfaceTypeId = ServletRequestUtils.getLongParameter(req, "surfaceTypeId");
                System.out.println("========== ClubAdminController.saveClub, surfaceTypeId = " + surfaceTypeId);

                if (surfaceTypeId != null) {
                    SurfaceType surfaceType = surfaceTypeManager.findSurfaceType(surfaceTypeId);
                    key = "surfaceTypeId";
                    value = surfaceType.getName();
                    field.setSurfaceType(surfaceType);
                    fieldManager.saveField(field);
                }
            }

            Long clubId = ServletRequestUtils.getLongParameter(req, "clubId");
            if (clubId != null) {
                Club club = clubManager.findClub(clubId);
                Region region = club.getRegion();

                System.out.println("region = " + region);

                String mail = ServletRequestUtils.getStringParameter(req, "mail");
                System.out.println("========== ClubAdminController.saveClub, mail=" + mail);
                if (mail != null) {
                    key = "mail";
                    value = mail;
                    club.setMail(mail);
                    clubManager.saveClub(club);
                } else {
                    String comments = ServletRequestUtils.getStringParameter(req, "comments");
                    System.out.println("========== ClubAdminController.saveClub, comments=" + comments);
                    if (comments != null) {
                        key = "comments";
                        value = comments;
                        club.setComments(comments);
                        clubManager.saveClub(club);
                    }
                }
            }

            String idxxx = ServletRequestUtils.getStringParameter(req, "idxxx");
            System.out.println("========== ClubAdminController.saveClub, idxxx="+idxxx);
            String select_1 = ServletRequestUtils.getStringParameter(req, "select_1");
            System.out.println("========== ClubAdminController.saveClub, select_1="+select_1);
            String clubIdxxx = ServletRequestUtils.getStringParameter(req, "clubId");
            System.out.println("========== ClubAdminController.saveClub, clubIdxxx="+clubIdxxx);
            String foo = ServletRequestUtils.getStringParameter(req, "foo");
            System.out.println("========== ClubAdminController.saveClub, foo="+foo);
            //value = ServletRequestUtils.getStringParameter(req, "value");
            //System.out.println("========== ClubAdminController.saveClub, value="+value);
            String dogName = ServletRequestUtils.getStringParameter(req, "dogName");
            System.out.println("========== ClubAdminController.saveClub, dogName="+dogName);
        } catch(Exception ex) {
            ex.printStackTrace();
        }

        //String regionIdString = "CEN-001";
        //Region region = regionManager.findByBusinessPK(regionIdString);
        //Region region = regionManager.findById(regionIdSelect);
        //System.out.println("region = " + region);

        ResponseMessage responseMessage = new ResponseMessage(key, value);
        Response response = new Response();
        response.addData(new Date(System.currentTimeMillis()));
        ModelAndView mav = new ModelAndView(JsonView.instance);
        //mav.addObject(JsonView.JSON_ROOT, response);
        //mav.addObject(JsonView.JSON_ROOT, "DDDDDDDD");
        mav.addObject(JsonView.JSON_ROOT, responseMessage);
        System.out.println("=========== org.springframework.prospring.ticket.web.controllers.ClubAdminController.updateClub  ModelAndView = " + mav);
        return mav;
    }

    
}
